package com.shujia.thread_pool;

import java.util.concurrent.*;

public class ThreadPoolDemo1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //newSingleThreadExecutor 创建的线程池里面只有一个线程
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();

        // Executors.newFixedThreadPool(n); 创建一个可重用固定线程数的线程池
        // ExecutorService threadPool = Executors.newFixedThreadPool(2);

        // Executors.newCachedThreadPool()：创建一个可根据需要创建新线程的线程池
        // 只要提交任务, 如果线程池有空闲的线程,会复用空闲的线程, 如果所有的线程都在处理任务
        // 它会新建线程并处理任务
        ExecutorService threadPool = Executors.newCachedThreadPool();

//        void execute(Runnable command) ：执行任务/命令，没有返回值，一般用来执行Runnable
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 2; i++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        });

//        Future<?> submit(Runnable task) ：返回null
        Future future = threadPool.submit(new Runnable() {
            @Override
            public void run() {
                for (char c = 'a'; c <= 'z'; c++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + c);
                }
            }
        });

        // 等待任务执行完成
        // future.get();
        // System.out.println("main");

        Thread.sleep(500);

//                <T> Future<T> submit(Callable<T> task)：执行任务，有返回值，一般又来执行Callable
        Future<String> callFuture = threadPool.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                for (char c = 'A'; c <= 'Z'; c++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + c);
                }

                return "success";
            }
        });

//        String result = callFuture.get();
//
//        System.out.println(result);


        //  <T> Future<T> submit(Runnable task, T result)：返回result
        Future<String> runFuture = threadPool.submit(new Runnable() {
            @Override
            public void run() {
                for (int i = 100; i < 150; i++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        }, "run success...");
        System.out.println(runFuture.get());


//        void shutdown() ：关闭连接池
        threadPool.shutdown();
        System.out.println("关闭线程池成功");

    }
}
